﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using SpringStudio.Common;
using SpringStudio.DAL;

namespace Sdepci.InfoCode.BLL.CodeDesign
{
    /// <summary>
    /// 编码规则管理类
    /// </summary>
    public class TypeRuleManager
    {
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="ruleName">规则名称</param>
        /// <param name="ruleDesc">中文描述</param>
        /// <param name="ruleModule">规则模型</param>
        /// <param name="ruleDemo">编码示例</param>
        /// <param name="seq">显示次序</param>
        /// <returns>是否成功</returns>
        public bool Add(string ruleName, string ruleDesc, string ruleModule, string ruleDemo, int seq)
        {
            try
            {
                string sql = string.Empty;
                string ruleGuid = Guid.NewGuid().ToString(); // 创建一个新的Guid
                sql = "Insert Into InfoCodeTypeRules (RuleGuid,RuleName,RuleDesc,RuleModule,RuleDemo,Seq) Values(@RuleGuid,@RuleName,@RuleDesc,@RuleModule,@RuleDemo,@Seq)";
                SqlHelper.ExecuteNonQuery(ConnectionString.Default, CommandType.Text, sql,
                    SqlHelper.NewSqlParameter("@RuleGuid", ruleGuid),
                    SqlHelper.NewSqlParameter("@RuleName", ruleName),
                    SqlHelper.NewSqlParameter("@RuleDesc", ruleDesc),
                    SqlHelper.NewSqlParameter("@RuleModule", ruleModule),
                    SqlHelper.NewSqlParameter("@RuleDemo", ruleDemo),
                    SqlHelper.NewSqlParameter("@Seq", seq));
                return true;
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
                return false;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ruleGuid">编码规则Guid</param>
        /// <returns>是否成功</returns>
        public bool Delete(string ruleGuid)
        {
            try
            {
                string sql = string.Empty;
                sql = "Delete From InfoCodeTypeRules Where RuleGuid=@RuleGuid";
                SqlHelper.ExecuteNonQuery(ConnectionString.Default, CommandType.Text, sql,
                    SqlHelper.NewSqlParameter("@RuleGuid", ruleGuid));
                return true;
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
                return false;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="ruleGuid">编码规则Guid</param>
        /// <param name="ruleName">规则名称</param>
        /// <param name="ruleDesc">中文描述</param>
        /// <param name="ruleModule">规则模型</param>
        /// <param name="ruleDemo">编码示例</param>
        /// <param name="seq">显示次序</param>
        /// <returns>是否成功</returns>
        public bool Update(string ruleGuid, string ruleName, string ruleDesc, string ruleModule, string ruleDemo, int seq)
        {
            try
            {
                string sql = string.Empty;
                sql = "Update InfoCodeTypeRules Set RuleName=@RuleName,RuleDesc=@RuleDesc,RuleModule=@RuleModule,RuleDemo=@RuleDemo,Seq=@Seq Where RuleGuid=@RuleGuid";
                SqlHelper.ExecuteNonQuery(ConnectionString.Default, CommandType.Text, sql,
                    SqlHelper.NewSqlParameter("@RuleGuid", ruleGuid),
                    SqlHelper.NewSqlParameter("@RuleName", ruleName),
                    SqlHelper.NewSqlParameter("@RuleDesc", ruleDesc),
                    SqlHelper.NewSqlParameter("@RuleModule", ruleModule),
                    SqlHelper.NewSqlParameter("@RuleDemo", ruleDemo),
                    SqlHelper.NewSqlParameter("@Seq", seq));
                return true;
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
                return false;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ruleGuid">编码规则Guid</param>
        /// <returns>是否存在</returns>
        public bool Exist(string ruleGuid)
        {
            try
            {
                string sql = string.Empty;
                sql = "Select RuleGuid From InfoCodeTypeRules Where RuleGuid=@RuleGuid";
                DataSet ds = SqlHelper.ExecuteDataSet(ConnectionString.Default, CommandType.Text, sql,
                    SqlHelper.NewSqlParameter("@RuleGuid", ruleGuid));
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
                return false;
            }
        }

        /// <summary>
        /// 获取规则列表（数据集）
        /// </summary>
        /// <returns></returns>
        public DataSet GetTypeRuleDs()
        {
            try
            {
                string sql = string.Empty;
                sql = "Select * From InfoCodeTypeRules Order by Seq Asc";
                return SqlHelper.ExecuteDataSet(ConnectionString.Default, CommandType.Text, sql);
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
                return null;
            }
        }

        /// <summary>
        /// 获取规则列表（字典）
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, string> GetTypeRuleDict()
        {
            Dictionary<string, string> dict = new Dictionary<string, string>();
            DataSet ds = this.GetTypeRuleDs();
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    dict.Add(row["RuleGuid"].ToString(), row["RuleName"].ToString());
                }
            }
            return dict;
        }
    }
}
